/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.method;

import com.inspur.edp.bef.core.be.BENodeEntity;
import com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.entityimpl.BefEntityImplGenCtx;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.utils.ExpressionUtils;
import com.inspur.edp.cef.api.attr.ChildEntityAttribute;
import com.inspur.edp.cef.api.message.CefException;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.overall.GeneratorUtil;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.ReturnStatement;
import org.eclipse.jdt.core.dom.StringLiteral;
import org.eclipse.jdt.core.dom.SwitchCase;
import org.eclipse.jdt.core.dom.SwitchStatement;
import org.eclipse.jdt.core.dom.ThrowStatement;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;

public class GetChildBEEntityGenerator extends ClassMethodGenerator {

  private BefEntityImplGenCtx entityImplGenCtx;
  private IGspCommonObject node;

  public GetChildBEEntityGenerator(BefEntityImplGenCtx entityImplGenCtx, IGspCommonObject node) {
    this.entityImplGenCtx = entityImplGenCtx;
    this.node = node;
  }

  private static final String paramchildCode = "childCode";
  private static final String paramchildid = "id";

  @Override
  protected java.util.ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    return new java.util.ArrayList<Modifier.ModifierKeyword>(java.util.Arrays
        .asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected String getMethodName() {
    return "innerGetChildBEEntity";
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    ArrayList<ParameterInfo> list = new ArrayList<>();
    ParameterInfo parameterInfo = new ParameterInfo();
    parameterInfo.setParamType(new TypeInfo(String.class));
    parameterInfo.setParamName(paramchildCode);
    list.add(parameterInfo);
    parameterInfo = new ParameterInfo();
    parameterInfo.setParamType(new TypeInfo(String.class));
    parameterInfo.setParamName(paramchildid);
    list.add(parameterInfo);
    return list;
  }

  @Override
  protected TypeInfo getReturnType() {
    return new TypeInfo(BENodeEntity.class);
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    SwitchStatement switchStatement = ast.newSwitchStatement();

    MethodInvocation methodInvocation = ast.newMethodInvocation();
    methodInvocation.setExpression(ast.newSimpleName(paramchildCode));
    methodInvocation.setName(ast.newSimpleName("toLowerCase"));
    switchStatement.setExpression(methodInvocation);
    addObjectCases(switchStatement);
    addDefaultCase(switchStatement);
    block.statements().add(switchStatement);
    return block;
  }

  private void addObjectCases(SwitchStatement switchStatement) {
    HashMap<String, MediateType> childTypes = entityImplGenCtx.getParentContext().getProjContext()
        .getEntityMediateTypes();
    if (childTypes == null || childTypes.size() == 0) {
      return;
    }
    if (node.getContainChildObjects().isEmpty()) {
      return;
    }
for (IGspCommonObject childNode : node.getContainChildObjects()) {
      addObjectCase(childNode.getCode(), childTypes.get(childNode.getCode()),
          switchStatement);
    }
  }

  private void addObjectCase(String nodeCode, MediateType MediateType,
      SwitchStatement switchStatement) {
    SwitchCase switchCase = ast.newSwitchCase();
    switchCase.setExpression(ExpressionUtils.getStringLiteral(ast, nodeCode.toLowerCase()));
    switchStatement.statements().add(switchCase);

    Block block = ast.newBlock();
    addNewEntityStatement(block, nodeCode, MediateType);
    //addsetEntntiyContextStatement(block, nodeCode, MediateType);
    //addSetEntityParentStatement(block, nodeCode, MediateType);
    addReturnEntityStatement(block, nodeCode, MediateType);
    switchStatement.statements().add(block);

  }

  private void addNewEntityStatement(Block block, String nodeCode, MediateType MediateType) {
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName("entity"));

    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newName(MediateType.getFullName())));
    classInstanceCreation.arguments().add(ast.newSimpleName(paramchildid));
    variableDeclarationFragment.setInitializer(classInstanceCreation);

    VariableDeclarationStatement variableDeclarationStatement = ast
        .newVariableDeclarationStatement(variableDeclarationFragment);
    variableDeclarationStatement.setType(ast.newSimpleType(ast.newName(MediateType.getFullName())));
    block.statements().add(variableDeclarationStatement);
  }

//  private void addsetEntntiyContextStatement(Block block, String nodeCode,
//      MediateType MediateType) {
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setName(ast.newSimpleName("setContext"));
//    methodInvocation.setExpression(ast.newSimpleName("entity"));
//
//    MethodInvocation methodInvocation1 = ast.newMethodInvocation();
//    methodInvocation1.setName(ast.newSimpleName("createChildEntityContext"));
//    methodInvocation1.setExpression(ast.newThisExpression());
//    StringLiteral stringLiteral = ast.newStringLiteral();
//    stringLiteral.setLiteralValue(nodeCode);
//    methodInvocation1.arguments().add(stringLiteral);
//    methodInvocation1.arguments().add(ast.newSimpleName(paramchildid));
//    methodInvocation.arguments().add(methodInvocation1);
//
//    block.statements().add(ast.newExpressionStatement(methodInvocation));
//
//  }

//  private void addSetEntityParentStatement(Block block, String nodeCode, MediateType MediateType) {
//    MethodInvocation methodInvocation = ast.newMethodInvocation();
//    methodInvocation.setName(ast.newSimpleName("setParent"));
//    methodInvocation.setExpression(ast.newSimpleName("entity"));
//    methodInvocation.arguments().add(ast.newThisExpression());
//
//    block.statements().add(ast.newExpressionStatement(methodInvocation));
//  }

  private void addReturnEntityStatement(Block block, String nodeCode, MediateType MediateType) {
    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName("entity"));
    block.statements().add(returnStatement);
  }

  private void addDefaultCase(SwitchStatement switchStatement) {
    SwitchCase case1 = ast.newSwitchCase();
    case1.setExpression(null);
    switchStatement.statements().add(case1);
    addImport(CefException.class.getTypeName());
    ThrowStatement throwStatement = GeneratorUtil.getThrowStatement(ast,null);
//    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
//    classInstanceCreation.setType(
//        new com.inspur.edp.caf.generator.baseInfo.TypeInfo(RuntimeException.class).getType(ast));
//    throwStatement.setExpression(classInstanceCreation);
    switchStatement.statements().add(throwStatement);
  }
}
